package org.eclipseuseradmin.internal.ui.handlers;

import java.util.Iterator;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipseuseradmin.ui.IRoleElement;

public class DeleteHandler extends AbstractHandler {

	public Object execute(ExecutionEvent event) throws ExecutionException {

		ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (selection instanceof TreeSelection) {

			Iterator<?> selectionItr = ((TreeSelection) selection).iterator();
			while (selectionItr.hasNext()) {
				IRoleElement roleElement = (IRoleElement) Platform
						.getAdapterManager().getAdapter(selectionItr.next(),
								IRoleElement.class);
				if (roleElement != null) {
					roleElement.delete();
				}
			}
		}
		return null;
	}

}
